import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.script.Constants;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSItemTile;
import com.speljohan.rsbot.script.wrappers.RSNPC;
import com.speljohan.rsbot.script.wrappers.RSPlayer;
import com.speljohan.rsbot.script.wrappers.RSTile;

public class Barrows extends Script implements PaintListener {
	public static final int[] bones = new int[] { 526 };
	public static final int[] Charms = new int[] { 12158, 12159, 12160, 12163 };
	public static final int[] monsters = new int[] { 4920, 4921, 2031, 2032,
			2033, 2034, 2035, 2036, 2037 };
	public static final int[] peach = new int[] { 6883 };
	public int atkexp;
	public int atkExp;
	public int btpused;
	int checkTime;
	public int defexp;
	public int defExp;
	public int eatAt = 43; // CHANGE BASED ON YOUR STATS. SHOULD BE AROUND
	// 40-50% OF YOUR HP
	public boolean emergency = false;
	int first;
	int GambleInt;
	public long hours;
	public int hpexp;
	public int hpExp;
	long lastCheck = 0;
	public boolean looting = false;
	public long minutes;
	public int oldatkExp;
	public int olddefExp;
	public int oldhpExp;
	public int oldrangedExp;
	public int oldstrExp;
	public int pee = 1;
	public int poop;
	int randomRunValue = 30;

	int randomTime = 0;
	public int rangedexp;
	public int rangedExp;
	int second;
	public long seconds;
	public int startatkExp;
	public int startdefExp;
	public int starthpExp;
	public int startpeaches;
	public int startrangedExp;
	public int startstrExp;
	public long starttime;
	public long startTime = System.currentTimeMillis();
	public int strexp;
	public int strExp;
	public int tabID = 8015; // Bones2Peaches
	public long time = System.currentTimeMillis();

	// Antiban -- Credits to Aelin
	public int AntiBan() {
		GambleInt = random(1, 5);
		if (GambleInt == 1) {
			wait(random(2000, 2000));
		}
		if (GambleInt == 2) {
			final int xA = random(0, 750);
			final int yA = random(0, 500);
			moveMouse(0, 0, xA, yA);
		}
		if (GambleInt == 3) {
			if (getCurrentTab() != Constants.TAB_INVENTORY) {
				openTab(Constants.TAB_INVENTORY);
			}
		}
		if (GambleInt == 4) {
			if (getMyPlayer().isMoving()) {
				return random(500, 1750);
			}

			if (System.currentTimeMillis() - lastCheck >= checkTime) {
				lastCheck = System.currentTimeMillis();
				checkTime = random(300000, 600000);

				if (getCurrentTab() != Constants.TAB_STATS) {
					openTab(Constants.TAB_STATS);
				}
				moveMouse(627, 240, 52, 29);
				return random(3000, 6000);
			}
		}

		return random(500, 1000);
	}

	public void begin() {
		if (startTime == 0) {
			startTime = System.currentTimeMillis();
			wait(400);
		}
	}

	public boolean clickInventoryItem(final int itemID, final boolean click) {
		if (getCurrentTab() != Constants.TAB_INVENTORY
				&& !RSInterface.getInterface(Constants.INTERFACE_BANK)
						.isValid()
				&& !RSInterface.getInterface(Constants.INTERFACE_STORE)
						.isValid()) {
			openTab(Constants.TAB_INVENTORY);
		}
		final int[] items = getInventoryArray();
		final java.util.List<Integer> possible = new ArrayList<Integer>();
		for (int i = 0; i < items.length; i++) {
			if (items[i] == itemID) {
				possible.add(i);
			}
		}
		if (possible.size() == 0) {
			return false;
		}
		final int idx = possible.get(random(0, possible.size()));
		final Point t = getInventoryItemPoint(idx);
		clickMouse(t, 5, 5, click);
		return true;
	}

	private void fightmonsters() {
		final RSPlayer myPlayer = getMyPlayer();
		if (!myPlayer.isInCombat()) {
			final RSNPC npc = getNearestNPCByID(Barrows.monsters);
			if (npc != null) {
				atNPC(npc, "Attack");
			}
		}
	}

	@Override
	public String getAuthor() {
		return "jameilious";
	}

	public void getback() {

		final RSPlayer myPlayer = getMyPlayer();
		if (!myPlayer.isInCombat()) {
			final int first = random(3534, 3536);
			final int second = random(9694, 9696); // randomized walking back to
			// center

			RSTile afksp0t;
			afksp0t = new RSTile(first, second);
			atTile(afksp0t, "Walk Here");

		}
	}

	@Override
	public String getName() {
		return "Barrows";
	}

	@Override
	public String getScriptCategory() {
		return "Combat";
	}

	@Override
	public String getScriptDescription() {
		String html = "";
		html += "<html>";
		html += "<body>";
		html += "Start in the rat room underneath barrows will fight monsters, take charms, use B2p. No randoms in barrows!";
		html += "</body>";
		html += "</html>";
		return html;

	}

	@Override
	public double getVersion() {
		return 1.0;
	}

	@Override
	public int loop() {
		final RSItemTile BONEZ = getGroundItemByID(526);
		if (BONEZ != null && !isInventoryFull()) { // Duhhh?
			atTile(BONEZ, "Take Bones");
			wait(random(800, 900));
		}
		takeCharms();
		wait(random(50, 100));
		PEACHES();
		wait(random(100, 150));
		fightmonsters();
		if (!isInventoryFull()) {
			wait(random(500, 600));
		}
		AntiBan();
		getback();
		wait(random(300, 400));
		PEACHES();
		fightmonsters();
		if (!isInventoryFull()) {
			wait(random(500, 600));
		}
		begin();

		if (BONEZ != null && !isInventoryFull()) {
			atTile(BONEZ, "Take Bones");
			wait(400);
		}
		fightmonsters();
		return random(10, 20);
	}

	@Override
	public void onFinish() {
		Bot.getEventManager().removeListener(PaintListener.class, this);
	}

	public void onRepaint(final Graphics g) {
		btpused = startpeaches - getInventoryCount(tabID);
		atkexp = skills.getCurrentSkillExp(Constants.STAT_ATTACK) - startatkExp;
		strexp = skills.getCurrentSkillExp(Constants.STAT_STRENGTH)
				- startstrExp;
		defexp = skills.getCurrentSkillExp(Constants.STAT_DEFENSE)
				- startdefExp;
		hpexp = skills.getCurrentSkillExp(Constants.STAT_HITPOINTS)
				- starthpExp;
		rangedexp = skills.getCurrentSkillExp(Constants.STAT_RANGE)
				- startrangedExp;
		time = System.currentTimeMillis() - startTime;
		seconds = time / 1000;
		if (seconds >= 60) {
			minutes = seconds / 60;
			seconds -= minutes * 60;
		}
		if (minutes >= 60) {
			hours = minutes / 60;
			minutes -= hours * 60;
		}
		// //////////////////////ATTACK///////////////////////////////////////
		if (startatkExp == 0) {
			startatkExp = skills.getCurrentSkillExp(Constants.STAT_ATTACK);
			oldatkExp = 0;
		}
		// ///////////////////////////////////////////////////////////////////////
		// //////////////////////STRENGTH///////////////////////////////////////
		if (startstrExp == 0) {
			startstrExp = skills.getCurrentSkillExp(Constants.STAT_STRENGTH);
			oldstrExp = 0;
		}
		// ///////////////////////////////////////////////////////////////////////
		// //////////////////////DEFENCE///////////////////////////////////////
		if (startdefExp == 0) {
			startdefExp = skills.getCurrentSkillExp(Constants.STAT_DEFENSE);
			olddefExp = 0;
		}
		// ///////////////////////////////////////////////////////////////////////
		// //////////////////////HP///////////////////////////////////////
		if (starthpExp == 0) {
			starthpExp = skills.getCurrentSkillExp(Constants.STAT_HITPOINTS);
			oldhpExp = 0;
		}
		// ///////////////////////////////////////////////////////////////////////
		// //////////////////////RANGED///////////////////////////////////////
		if (startrangedExp == 0) {
			startrangedExp = skills.getCurrentSkillExp(Constants.STAT_RANGE);
			oldrangedExp = 0;
		}
		// ///////////////////////////////////////////////////////////////////////
		// /////////////////////PEACHES////////////////////////////////////////////
		if (startpeaches == 0) {
			startpeaches = getInventoryCount(tabID);
		}
		g.setColor(Color.GREEN);
		g.drawString("Jameilious' Barrows Afker", 260, 20);

		g.drawString("Time running: " + hours + " hours " + minutes
				+ " minutes " + seconds + " seconds", 260, 146);
		g.drawString("Attack exp gained: " + atkexp, 260, 38);
		g.drawString("strength exp gained: " + strexp, 260, 56);
		g.drawString("defence exp gained: " + defexp, 260, 74);
		g.drawString("HP exp gained: " + hpexp, 260, 92);
		g.drawString("ranged exp gained: " + rangedexp, 260, 110);
		g.drawString("BTP USED: " + btpused, 260, 128);
	}

	@Override
	public boolean onStart(final String[] args) {
		return true;
	}

	public void PEACHES() {
		poop = random(25, 65);
		if (skills.getCurrentSkillLevel(3) <= eatAt + random(-5, 5)) // Randomizes
		// the
		// eatAt
		// by 10
		// -5 to
		// +5
		{
			if (skills.getCurrentSkillLevel(3) <= eatAt + random(-5, 5)) {
				log.info("Food time");
				if (getInventoryCount(Barrows.peach) >= 1) {
					atInventoryItem(6883, "eat");
					wait(random(1000, 2000));
				} else {
					log.info("Emergency tab time");
					clickInventoryItem(tabID, true);
					wait(random(2000, 3000));
				}
				if (getInventoryCount(Barrows.bones) >= 15 || isInventoryFull()
						&& inventoryContains(526)) // Safe
				{
					if (getInventoryCount(tabID) >= 1) {
						clickInventoryItem(tabID, true);
						log.info("Made more peaches");
						wait(random(2000, 3000));
					} else {
						log.info("We are out of tablets!");
						teleport();
						wait(5000);
						stopAllScripts();
					}
				}
			}
		}
	}

	public void takeCharms() {
		final RSItemTile charm = getGroundItemByID(Barrows.Charms);
		if (charm != null) {
			for (final int charm2 : Barrows.Charms) {
				atTile(getGroundItemByID(charm2), "Charm");
			}
		}
	}

	public void teleport() {
		if (getCurrentTab() != Constants.TAB_MAGIC) {
			openTab(Constants.TAB_MAGIC);
			castSpell(16); //
			castSpell(16); // Should we mess up
			castSpell(16); //
			log.info("Teleported to varrock");
		}
	}
}